﻿namespace KeRui.DAL
{
    using KeRui.DBUtility;
    using KeRui.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;

    public class SysConfig
    {
        public DataSet GetList(string Item, int IsUsing)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@Item", SqlDbType.VarChar, 50), new SqlParameter("@IsUsing", SqlDbType.Int, 4) };
            parameters[0].Value = Item;
            parameters[1].Value = IsUsing;
            return DbHelperSQL.RunProcedure("SysConfig_GetList", parameters, "ds");
        }

        public KeRui.Model.SysConfig GetModel(string Item, string Key)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@Item", SqlDbType.VarChar, 50), new SqlParameter("@Key", SqlDbType.VarChar, 50) };
            parameters[0].Value = Item;
            parameters[1].Value = Key;
            KeRui.Model.SysConfig config = new KeRui.Model.SysConfig();
            DataSet set = DbHelperSQL.RunProcedure("SysConfig_GetModel", parameters, "ds");
            if (set.Tables[0].Rows.Count > 0)
            {
                config.Item = set.Tables[0].Rows[0]["Item"].ToString();
                config.Key = set.Tables[0].Rows[0]["Key"].ToString();
                config.Name = set.Tables[0].Rows[0]["Name"].ToString();
                config.Value = set.Tables[0].Rows[0]["Value"].ToString();
                if (set.Tables[0].Rows[0]["OrderID"].ToString() != "")
                {
                    config.OrderID = int.Parse(set.Tables[0].Rows[0]["OrderID"].ToString());
                }
                if (set.Tables[0].Rows[0]["IsUsing"].ToString() != "")
                {
                    config.IsUsing = int.Parse(set.Tables[0].Rows[0]["IsUsing"].ToString());
                }
                config.Note = set.Tables[0].Rows[0]["Note"].ToString();
                return config;
            }
            return null;
        }

        public void Update(KeRui.Model.SysConfig model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@Item", SqlDbType.VarChar, 50), new SqlParameter("@Key", SqlDbType.VarChar, 50), new SqlParameter("@Name", SqlDbType.NVarChar, 50), new SqlParameter("@Value", SqlDbType.NText), new SqlParameter("@OrderID", SqlDbType.Int, 4), new SqlParameter("@IsUsing", SqlDbType.Int, 4), new SqlParameter("@Note", SqlDbType.NVarChar, 0xff) };
            parameters[0].Value = model.Item;
            parameters[1].Value = model.Key;
            parameters[2].Value = model.Name;
            parameters[3].Value = model.Value;
            parameters[4].Value = model.OrderID;
            parameters[5].Value = model.IsUsing;
            parameters[6].Value = model.Note;
            DbHelperSQL.RunProcedure("SysConfig_Update", parameters, out num);
        }
    }
}

